package com.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.common.utils.Result;
import com.entity.Operation;
import com.entity.TaskRecord;
import com.query.TaskRecordQuery;
import com.service.OperationService;
import com.service.TaskRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 *
 * @author wty
 * @since 2021-03-18
 */
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
@RestController
@RequestMapping("/taskRecord")
public class TaskRecordController {
    @Autowired
    private TaskRecordService taskRecordService;

    @Autowired
    private OperationService operationService;

    /**
     * 根据条件获取任务数据
     */
    @RequestMapping("/getTaskDataByTaskId")
    public Result getTaskDataByTaskId(@RequestBody TaskRecord taskRecord ){
        return  taskRecordService.getTaskDataByTaskId(taskRecord);
    }

    /**
     * 根据条件更新任务数据
     */
    @RequestMapping("/updateTaskRecord")
    public Result updateTaskRecord (@RequestBody TaskRecord taskRecord ){

        boolean flag = taskRecordService.updateById(taskRecord);

        if(!StringUtils.isEmpty(taskRecord.getOperation())){
            if(taskRecord.getOperation().equals("add")){
                //增加相应的日志记录
                Operation operation =new Operation();
                operation.setAddTime(new Date().getTime()/1000);
                operation.setStatus(1);
                operation.setTaskStoreId(taskRecord.getTaskId());
                operationService.insert(operation);
            }else{
                //删除相应的日志记录
                Wrapper<Operation> owr=new EntityWrapper<>();
                 owr.eq("task_store_id",taskRecord.getTaskId())
                    .eq("status",1)
                    .orderBy("add_time", false)
                    .last("limit 1");
                Operation operation = operationService.selectOne(owr);
                if(null!=operation){
                    operation.setStatus(0);
                    operationService.updateById(operation);
                }
            }
        }


        if(flag){
            return  Result.success("更新成功");
        }else {
            return  Result.error("更新失败");
        }
    }

    /**
     * 根据条件获取最近N天的数据
     */
    @RequestMapping("/getTaskRecordDataList")
    public Result getTaskRecordDataList(@RequestBody TaskRecordQuery taskRecordQuery ){
        int dayNum = taskRecordQuery.getDayNum();
        int taskId = taskRecordQuery.getTaskId();

        Page<TaskRecord> page=new Page<>();
        page.setSize(14);
        page.setCurrent(1);
        Wrapper<TaskRecord> wr=new EntityWrapper<>();
        wr.eq("task_id",taskId);
        wr.orderBy("add_time",false);
        Page<TaskRecord> taskRecordPage = taskRecordService.selectPage(page, wr);
        return Result.success(taskRecordPage);
    }
}

